package com.ruoyi.web.controller.shop.service.Impl;

import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.entity.DO.member.MemberVipBenefitDO;
import com.ruoyi.common.entity.DO.member.MemberVipBenefitExtDO;
import com.ruoyi.common.entity.VO.member.MemberVipBenefitExtVO;
import com.ruoyi.common.entity.VO.member.MemberVipBenefitSearchVO;
import com.ruoyi.common.entity.VO.member.MemberVipBenefitVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.web.controller.shop.mapper.IBackShopMemberVipBenefitExtMapper;
import com.ruoyi.web.controller.shop.mapper.IBackShopMemberVipBenefitMapper;
import com.ruoyi.web.controller.shop.service.IBackShopMemberVipBenefitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BackShopMemberVipBenefitServiceImpl implements IBackShopMemberVipBenefitService {

    @Resource
    private IBackShopMemberVipBenefitMapper memberVipBenefitMapper;

    @Resource
    private IBackShopMemberVipBenefitExtMapper memberVipBenefitExtMapper;

    @Override
    public List<MemberVipBenefitVO> searchList(MemberVipBenefitSearchVO searchVO) {
        return memberVipBenefitMapper.searchList(searchVO);
    }

    @Override
    public List<MemberVipBenefitExtVO> searchCustomList(MemberVipBenefitSearchVO searchVO) {
        return memberVipBenefitMapper.searchCustomList(searchVO);
    }

    @Override
    public void edit(MemberVipBenefitVO editVO) {
        if(null == editVO.getId() || 0L == editVO.getId()){
            throw new BusinessException("ID为空！");
        }
        if(null == editVO.getSystemModule()){
            throw new BusinessException("systemModule为空！");
        }
        MemberVipBenefitDO benefitInfo = memberVipBenefitMapper.selectByPrimaryKey(editVO.getId());
        if(null == benefitInfo || 1 == benefitInfo.getIsDelete()){
            throw new BusinessException("权益不存在！");
        }
        if(null != editVO.getBenefitAmount()){
            benefitInfo.setIsAmount(1);
            benefitInfo.setBenefitAmount(editVO.getBenefitAmount());
        }
//        benefitInfo.setIsEnable(editVO.getIsEnable());
//        benefitInfo.setIsCredit(editVO.getIsCredit());
//        if(SysConfigConstants.SystemModuleEnum.PETROL.getValue() == editVO.getSystemModule()){
//            benefitInfo.setIsDiscount(editVO.getIsDiscount());
//            benefitInfo.setBenefitDiscountRate(editVO.getBenefitDiscountRate());
//            benefitInfo.setBenefitCreditRate(editVO.getBenefitCreditRate());
//        }else{
//            benefitInfo.setBenefitCredit(editVO.getBenefitCredit());
//        }
        memberVipBenefitMapper.updateByPrimaryKeySelective(benefitInfo);
    }

    @Override
    public void editExt(MemberVipBenefitExtVO editVO) {
        if(null == editVO.getMemberId() || 0L == editVO.getMemberId()){
            throw new BusinessException("请指定会员！");
        }
        if(null == editVO.getProductId() || 0L == editVO.getProductId()){
            throw new BusinessException("请指定油品！");
        }
        if(null == editVO.getSystemModule()){
            throw new BusinessException("systemModule为空！");
        }
        MemberVipBenefitExtDO benefitInfo = memberVipBenefitExtMapper.selectOneForAU(editVO.getMemberId(),editVO.getProductId());
        if(null == benefitInfo){
            //新增进去
            MemberVipBenefitExtDO newBenefit = new MemberVipBenefitExtDO();
            newBenefit.setBenefitDiscountRate(editVO.getBenefitDiscountRate());
            newBenefit.setShopId(editVO.getShopId());
            newBenefit.setSystemModule(editVO.getSystemModule());
            newBenefit.setMemberId(editVO.getMemberId());
            newBenefit.setProductId(editVO.getProductId());
            newBenefit.setIsEnable(1);
            newBenefit.setIsDelete(0);
            newBenefit.setCreateTime(new Date());
            newBenefit.setCreateBy(editVO.getUpdateBy());
            memberVipBenefitExtMapper.insert(newBenefit);
        }else{
            //更新
            benefitInfo.setBenefitDiscountRate(editVO.getBenefitDiscountRate());
            benefitInfo.setIsEnable(editVO.getIsEnable());
            benefitInfo.setUpdateTime(new Date());
            benefitInfo.setUpdateBy(editVO.getUpdateBy());
            memberVipBenefitExtMapper.updateByPrimaryKeySelective(benefitInfo);
        }
    }
}
